package com.hzit.servlet.emp;

import com.hzit.bean.Emp;
import com.hzit.dao.EmpDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "UpdateEmpServlet", urlPatterns = "/emp/update")
public class UpdateEmpServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编号
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");


        //2.获取需要添加数据

        int empno = Integer.parseInt(request.getParameter("empno"));
        String ename = request.getParameter("ename");
        String job = request.getParameter("job");
        Integer mgr = Integer.parseInt(request.getParameter("mgr"));
        String hireDate = request.getParameter("hireDate");

        //字符串转日期
        Date mydate = null;
        try {
            mydate = new SimpleDateFormat("yyyy-MM-dd").parse(hireDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Double sal = Double.parseDouble(request.getParameter("sal"));
        Integer deptno = Integer.parseInt(request.getParameter("deptno"));

        Emp emp = new Emp(empno, ename, job, mgr, mydate, sal, deptno);

        //3.调用jdbc,获取数据
        EmpDao empDao = new EmpDao();
        int row = empDao.updateEmp(emp);
        System.out.println("修改数据row:" + row);

        //4.删除一样，重新查一遍
        response.sendRedirect(request.getContextPath()+"/emp/all");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
